package cn.yunhe.controller;

import cn.yunhe.entity.User;
import cn.yunhe.service.IUserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

// 用户注册
@WebServlet("/before/InsertUserServlet")
public class InsertUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                    IUserService iUserService = new IUserService();
                         User user = new User();
                     PrintWriter out = response.getWriter();
                      String userName =     request.getParameter("uid");
                     boolean num =  iUserService.setUserName(userName);
                     if (num){
                         response.setContentType("text/html;charset=UTF-8");
                         out.print("<script>alert('该账号已经注册过了');location.href='register.html'</script>");
                     }else {
                         String userPass =     request.getParameter("psw1");
                         String phone    =     request.getParameter("phone");
                         String address  =     request.getParameter("address");
                         user.setUserName(userName);
                         user.setUserPass(userPass);
                         user.setTelphone(phone);
                         user.setAddress(address);
                         boolean result  =  iUserService.inserUser(user);
                         if (result){
                                response.sendRedirect("login.html");
                             }
                     }

    }



}
